//
//  WiteBoxTestsViewController.m
//  WiteBox
//
//  Created by John Blanco on 6/28/09.
//  Copyright 2009 Effective UI. All rights reserved.
//

#import "WiteBoxTestsViewController.h"

#import "TestRun.h"
#import "TestCaseRun.h"
#import "AssertionException.h"
#import "WiteBoxTestDetailViewController.h"
#import "WiteBoxStyles.h"

@implementation WiteBoxTestsViewController

@synthesize testCaseRun;

/*
 - (id)initWithStyle:(UITableViewStyle)style {
 // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
 if (self = [super initWithStyle:style]) {
 }
 return self;
 }
 */

- (void)viewDidLoad {
    [super viewDidLoad];
	
	self.title = [NSString stringWithFormat:@"%@ v%@", [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleName"], [[NSBundle mainBundle] objectForInfoDictionaryKey: @"CFBundleVersion"]];
	self.view.backgroundColor = [WiteBoxStyles backgroundColor];
	self.navigationItem.backBarButtonItem =
	[[UIBarButtonItem alloc] initWithTitle:@"Back"
									 style: UIBarButtonItemStyleBordered
									target:nil
									action:nil];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
	[self.tableView reloadData];
}

/*
 - (void)viewDidAppear:(BOOL)animated {
 [super viewDidAppear:animated];
 }
 */
/*
 - (void)viewWillDisappear:(BOOL)animated {
 [super viewWillDisappear:animated];
 }
 */
/*
 - (void)viewDidDisappear:(BOOL)animated {
 [super viewDidDisappear:animated];
 }
 */

/*
 // Override to allow orientations other than the default portrait orientation.
 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 // Return YES for supported orientations
 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 }
 */

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

/*
 // Override to support conditional editing of the table view.
 - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the specified item to be editable.
 return YES;
 }
 */


/*
 // Override to support editing the table view.
 - (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
 
 if (editingStyle == UITableViewCellEditingStyleDelete) {
 // Delete the row from the data source
 [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
 }   
 else if (editingStyle == UITableViewCellEditingStyleInsert) {
 // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
 }   
 }
 */


/*
 // Override to support rearranging the table view.
 - (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
 }
 */


/*
 // Override to support conditional rearranging of the table view.
 - (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
 // Return NO if you do not want the item to be re-orderable.
 return YES;
 }
 */


- (void)dealloc {
	[testCaseRun release];
	[witeBoxTestDetailViewController release];
    [super dealloc];
}

#pragma mark -

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
	UIView *baseView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 30)];
	UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 300, 20)];
	headerLabel.textColor = [WiteBoxStyles tableHeaderColor];
	headerLabel.shadowColor = [WiteBoxStyles tableHeaderShadowColor];
	headerLabel.text = [testCaseRun testCaseClassName];
	headerLabel.textAlignment = UITextAlignmentCenter;
	headerLabel.backgroundColor = [UIColor clearColor];
	headerLabel.font = [UIFont boldSystemFontOfSize:18];
	
	[baseView addSubview:headerLabel];
	
	return baseView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
	return 40;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return (testCaseRun) ? [testCaseRun executedTests] : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *CellIdentifier = @"TestCell";
	
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:CellIdentifier] autorelease];
		cell.textLabel.font = [UIFont systemFontOfSize:14.0];
		cell.detailTextLabel.font = [UIFont systemFontOfSize:12.0];
    }
	
	TestRun *testRun = (TestRun *)[[testCaseRun testRuns] objectAtIndex:indexPath.row]; 
	
	cell.backgroundColor = [WiteBoxStyles cellBackgroundColor];
	cell.textLabel.textColor = [WiteBoxStyles cellTextColor];
	cell.textLabel.text = [testRun testMethodName];
	
	if (!testRun.isTestComplete) {
		cell.textLabel.textColor = [WiteBoxStyles cellTextColor];
		cell.accessoryType = UITableViewCellAccessoryNone;
		cell.detailTextLabel.text = @"-";
		cell.selectionStyle = UITableViewCellSelectionStyleNone;
	} else {
		if (testRun.isPassingTest) {
			cell.textLabel.textColor = [WiteBoxStyles greenCorrectDarkerContrast];
			cell.detailTextLabel.textColor = [WiteBoxStyles cellTextDetailColor];
			cell.accessoryType = UITableViewCellAccessoryNone;
			cell.detailTextLabel.text = [NSString stringWithFormat:@"(%0.2fs) Passed", testRun.duration];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
		} else {
			cell.textLabel.textColor = [WiteBoxStyles redWrongDarkerContrast];
			cell.detailTextLabel.textColor = [WiteBoxStyles cellTextDetailColor];
			cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
			cell.detailTextLabel.text = [NSString stringWithFormat:@"(%0.2fs) %@%@", testRun.duration, [testRun exceptionFilename] ? [testRun exceptionFilename] : @"Error", [testRun exceptionLineNumber] >= 0 ? [NSString stringWithFormat:@": Line %i", [testRun exceptionLineNumber]] : @""];
			cell.selectionStyle = UITableViewCellSelectionStyleNone;
		}
	}
	
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	TestRun *testRun = (TestRun *)[[testCaseRun testRuns] objectAtIndex:indexPath.row]; 
	
	if (![testRun isPassingTest]) {
		if (!witeBoxTestDetailViewController) {
			witeBoxTestDetailViewController = [[WiteBoxTestDetailViewController alloc] init];
		}
		
		[self.navigationController pushViewController:witeBoxTestDetailViewController animated:YES];
		[witeBoxTestDetailViewController setTestRun:(TestRun *)[[testCaseRun testRuns] objectAtIndex:indexPath.row]];
	}
}


@end

